﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace CIT.WMS.TranseferRM
{
    public partial class FrmTranseferIn : Form
    {
        public FrmTranseferIn()
        {
            InitializeComponent();
        }

        DataTable DtIn;

        string realLocationSN;
        string InStorageSN;
        string InAreaSN;
        string partNumber;
        //string dataCode;
        string version;

        private void cmb_BillNo_SelectedIndexChanged(object sender, EventArgs e)
        {
            InputBillNo();
        }

        private void cmb_BillNo_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                InputBillNo();
            }
        }

        private void txt_LocationSN_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar != 13)
            {
                return;
            }
            try
            {
                if (string.IsNullOrEmpty(cmb_BillNo.Text))
                {
                    MessageBox.Show("请先输入单号!");
                    return;
                }

                if (IsDateTime(txtDateCode.Text) == false)
                {
                    MessageBox.Show("生产日期格式错误（例:20190501）");
                    txtDateCode.Text = "";
                    return;
                }

                realLocationSN = txt_LocationSN.Text;

                try
                {
                    var snArray = realLocationSN.Split('#');

                    var storageSN = snArray[0];
                    var aresSN = snArray[0] + '#' + snArray[1];



                    if (!string.IsNullOrEmpty(InStorageSN) && !InStorageSN.Equals(storageSN))
                    {
                        MessageBox.Show("上架仓库" + InStorageSN + "与扫描的仓库" + storageSN + "不一致！");
                        return;
                    }

                    if (!string.IsNullOrEmpty(InAreaSN) && !InAreaSN.Equals(aresSN))
                    {
                        MessageBox.Show("上架库区" + InAreaSN + "与扫描的库区" + aresSN + "不一致！");
                        return;
                    }


                    txt_LocationSN.Text = snArray[1] + snArray[2];
                }
                catch
                {
                    MessageBox.Show("库位" + realLocationSN + "格式不正确，请重新输入!");
                    txt_LocationSN.Text = "";
                    return;
                }

                if (string.IsNullOrEmpty(txt_Qty.Text))
                {
                    MessageBox.Show("移库单" + cmb_BillNo.Text + "临时库存不足，请选择其他移库单！");
                    return;
                }

                var InveDT = Bll_Transefer.GetInventoryInfo(realLocationSN, partNumber);
                bool isNew = true;
                if (InveDT.Rows.Count > 0)
                {
                    //if (!partNumber.Equals(InveDT.Rows[0]["PartNumber"].ToString()))
                    //{
                    //    MessageBox.Show("上架库位的物料号" + InveDT.Rows[0]["PartNumber"].ToString() + "与移库的物料号" + partNumber + "不一致!");
                    //    txt_LocationSN.Text = "";
                    //    return;
                    //}
                    if (!version.Equals(InveDT.Rows[0]["Version"].ToString()))
                    {
                        MessageBox.Show("上架库位的版本" + InveDT.Rows[0]["Version"].ToString() + "与移库的版本" + version + "不一致!");
                        txt_LocationSN.Text = "";
                        return;
                    }
                    if (!txtDateCode.Text.Equals(InveDT.Rows[0]["DateCode"].ToString()))
                    {
                        MessageBox.Show("上架库位的DateCode" + InveDT.Rows[0]["DateCode"].ToString() + "与移库的DateCode" + txtDateCode.Text + "不一致!");
                        txt_LocationSN.Text = "";
                        return;
                    }

                    isNew = false;
                }

                MessageBoxButtons messButton = MessageBoxButtons.OKCancel;

                DialogResult dr = MessageBox.Show("本次操作的上架库位：" + txt_LocationSN.Text + ", 请确认！", "上架确认", messButton, MessageBoxIcon.None, MessageBoxDefaultButton.Button1);

                if (dr == DialogResult.OK)
                {
                    var qty = Convert.ToDecimal(txt_Qty.Text);

                    var isSuccess = Bll_Transefer.TransferIn(cmb_BillNo.Text, realLocationSN, qty, isNew, partNumber,txtDateCode.Text);

                    if (isSuccess)
                    {
                        lab_msg.Text = cmb_BillNo.Text + "上架成功!\r\n请选择其他单号！";

                        BindCMB();
                        cmb_BillNo.Focus();
                        txt_LocationSN.Text = "";
                        txt_Qty.Text = "";

                    }
                    else
                    {
                        MessageBox.Show(cmb_BillNo.Text + "上架失败!" + txt_LocationSN.Text + "库位库存已被使用！");
                        txt_LocationSN.Text = "";
                    }
                }
                else
                {
                    txt_LocationSN.Text = "";
                    txt_LocationSN.Focus();
                }
            }
            catch 
            {
                MessageBox.Show(cmb_BillNo.Text + "上架失败!数据有错误！");
                txt_LocationSN.Text = "";
                txt_LocationSN.Focus();
            }
        }

        /// <summary>
        /// 判断时间格式
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        private bool IsDateTime(string str)
        {
            System.IFormatProvider format = new System.Globalization.CultureInfo("zh-CN", true);
            try
            {
                DateTime.ParseExact(str, "yyyyMMdd", format);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public void BindCMB()
        {
            try
            {
                //status：1为下架成功的移库单，需要上架 
                DtIn = Bll_Transefer.GetTransferDT("1");

                cmb_BillNo.Text = "";
                cmb_BillNo.Items.Clear();

                if (DtIn.Rows.Count > 0)
                {
                    foreach (DataRow dr in DtIn.Rows)
                    {
                        cmb_BillNo.Items.Add(dr["BillNo"].ToString());
                    }
                }
            }
            catch 
            {
                MessageBox.Show("订单号下拉框数据有错误！");
            }
        }

        public void InputBillNo()
        {
            try
            {
                DataRow[] drIn = DtIn.Select(string.Format(" BillNo = '{0}'", cmb_BillNo.Text));
                if (drIn.Length > 0)
                {

                    DataTable info = Bll_Transefer.GetTransferInfo(cmb_BillNo.Text);
                    if (info.Rows.Count > 0)
                    {
                        //if (string.IsNullOrEmpty(info.Rows[0]["Updator"].ToString()) || Utils.uContext.UserNamek__BackingField.Equals(info.Rows[0]["Updator"].ToString()))
                        //{

                        //txt_LocationSN.Text = info.Rows[0]["OutLocation_SN"].ToString();
                        //txt_Qty.Focus();

                        var inv = Bll_Transefer.GetInventoryInfo(cmb_BillNo.Text);
                        if (inv.Rows.Count > 0)
                        {

                            txt_Qty.Text = inv.Rows[0]["Qty"].ToString();
                            

                            InStorageSN = info.Rows[0]["InStorage_SN"].ToString();
                            InAreaSN = info.Rows[0]["InArea_SN"].ToString();
                            partNumber = info.Rows[0]["PartNumber"].ToString();
                            txtDateCode.Text = info.Rows[0]["DateCode"].ToString();
                            version = info.Rows[0]["Version"].ToString();
                            lab_msg.Text = "仓库:" + InStorageSN + "\r\n物料号:" + partNumber;
                            if (string.IsNullOrEmpty(txtDateCode.Text))
                            {
                                txtDateCode.Enabled = true;
                                txtDateCode.Focus();
                            }
                            else
                            {
                                txtDateCode.Enabled = false;
                                txt_LocationSN.Focus();
                            }
                        }
                        else
                        {
                            MessageBox.Show("移库单" + cmb_BillNo.Text + "临时库存不足，请选择其他移库单！");
                            cmb_BillNo.Text = "";
                            cmb_BillNo.Focus();
                        }
                        //}
                        //else
                        //{
                        //    MessageBox.Show(cmb_BillNo.Text + "已被" + info.Rows[0]["Updator"].ToString() + "操作，请选择其他单号!");

                        //    cmb_BillNo.Text = "";
                        //    txt_LocationSN.Text = "";
                        //    txt_Qty.Text = "";
                        //}
                    }
                }
                else
                {
                    //lab_msg.Text = cbx_Doc.Text + "单号不正确,请输入正确的单据!";
                    MessageBox.Show(cmb_BillNo.Text + "单号不正确,请输入正确的单据!");
                    cmb_BillNo.Text = "";
                    cmb_BillNo.Focus();
                    
                 

                }
            }
            catch 
            {
                MessageBox.Show("数据有错误!");
            }
        }

        private void FrmTranseferIn_Load(object sender, EventArgs e)
        {
            BindCMB();
        }

        private void txtDateCode_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                if (IsDateTime(txtDateCode.Text) == false)
                {
                    MessageBox.Show("生产日期格式错误（例:20190501）");
                }
            }
        }

    }
}